export default {
  reduceCartitem(state, id) {
    state.cart = state.cart.reduce((result, item) => {
      if (item.id === id) {
        item.buyNum--
      }
      if (item.buyNum > 0) {
        result.push(item)
      }
      return result
    }, [])
  },
  addCatitem(state, a) {
    console.log(a,"xcq")
    state.cart = state.cart.map(item => {
      console.log(item)
      if (item.id === a.id) {
        item.buyNum += a.buyNum
      }
      return item
    })
    console.log()
    
  },
  detelecat(state, id) {
    state.cart = state.cart.filter(item => item.id !== id)
  },

  addcartxc(state, item) {
    //你这相当于只传了一个id  你需要传ite
    console.log(item.id,'aa')
    const {
      id,
    } = item
    const addcartItem = state.cart.some((addcartid) => {
      return addcartid.id === id
    })
    if (addcartItem) {
      this.commit('addCatitem',item )
      console.log(addcartItem)
    } else {
      state.cart.push({
        ...item,
        buyNum: 1,
        ischecked: false
      })
    }
  },
  loginSuccess(state, users) {
    state.users = {
      ...users,
      isLogin: true
    }
    console.log(state.users)
  },
  loginout(state, users) {
    state.users = {
      ...users,
      isLogin: false
    }
  }
}
